package com.tma.mylocation;

import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class MyLocationActivity extends MapActivity implements LocationListener {

	private LocationManager mLocationManager;
	private MapView mMapView;
	private MyLocationOverlay mMyLocationOverlay;

	private TextView mLatitude;
	private TextView mLongitude;
	private TextView mAccuracy;
	private TextView mAltitude;
	private TextView mBearing;
	private TextView mSpeed;
	private TextView mTime;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_location);

		mMapView = (MapView) findViewById(R.id.mapview);
		mMapView.setBuiltInZoomControls(true);

		mLatitude = (TextView) findViewById(R.id.latitute);
		mLongitude = (TextView) findViewById(R.id.longitute);
		mAccuracy = (TextView) findViewById(R.id.accuracy);
		mAltitude = (TextView) findViewById(R.id.altitude);
		mBearing = (TextView) findViewById(R.id.bearing);
		mSpeed = (TextView) findViewById(R.id.speed);
		mTime = (TextView) findViewById(R.id.time);

		mMyLocationOverlay = new MyLocationOverlay(this, mMapView);

		mMyLocationOverlay.runOnFirstFix(new Runnable() {
			public void run() {
				mMapView.getController().animateTo(
						mMyLocationOverlay.getMyLocation());
			}
		});

		List<Overlay> mapOverlays = mMapView.getOverlays();
		mapOverlays.add(mMyLocationOverlay);

		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		boolean enabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		// Better solution would be to display a dialog and suggesting to
		// go to the settings
		if (!enabled) {
			Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
			startActivity(intent);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_my_location, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_trace_me:
			mMapView.getController().animateTo(mMyLocationOverlay.getMyLocation());
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}		
	}

	@Override
	protected void onPause() {
		super.onPause();
		mLocationManager.removeUpdates(this);
		mMyLocationOverlay.disableCompass();
		mMyLocationOverlay.disableMyLocation();
	}

	@Override
	protected void onResume() {
		super.onResume();
		// Register the listener with the Location Manager to receive location updates
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);

		mMyLocationOverlay.enableCompass();
		mMyLocationOverlay.enableMyLocation();
	}

	@Override
	public void onLocationChanged(Location location) {
		
		mLatitude.setText("Latitude: " + location.getLatitude());
		mLongitude.setText("Longitude: " + location.getLongitude());
		if (location.hasAccuracy()) {
			mAccuracy.setText("Accuracy: " + location.getAccuracy() + " m");
		}
		if (location.hasAltitude()) {
			mAltitude.setText("Altitude: " + location.getAltitude());
		}
		if (location.hasBearing()) {
			mBearing.setText("Bearing: " + location.getBearing());
		}
		mSpeed.setText("Speed: " + location.getSpeed() + " m/s");
		mTime.setText("Time: " + new Date(location.getTime())); 
	}

	@Override
	public void onProviderDisabled(String provider) {
		Toast.makeText(this, "Disabled provider " + provider,
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onProviderEnabled(String provider) {
		Toast.makeText(this, "Enabled provider " + provider,
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
